package com.project;

import com.project.servlet.*;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    private ApplicationContext context = new AnnotationConfigApplicationContext(MyBatisConfig.class);

    public Main(){
        Tomcat tomcat = new Tomcat();
        //开放8088端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //创建上下文对象
        Context ctx = tomcat.addContext("",null);

        //注册servelt
        Wrapper wrp = Tomcat.addServlet(ctx,"test",new TestServlet());
        //容器启动时，完成初始化
        wrp.setLoadOnStartup(1);
        //设置servlet请求路径
        wrp.addMapping("/test");

        //注册servelt
        Wrapper wrp1 = Tomcat.addServlet(ctx,"findAll",context.getBean(FindAllServlet.class));
        //容器启动时，完成初始化
        wrp1.setLoadOnStartup(1);
        //设置servlet请求路径
        wrp1.addMapping("/findAll");

        //注册servelt
        Wrapper wrp2 = Tomcat.addServlet(ctx,"add",context.getBean(AddServlet.class));
        //容器启动时，完成初始化
        wrp2.setLoadOnStartup(1);
        //设置servlet请求路径
        wrp2.addMapping("/add");

        //注册servelt
        Wrapper wrp3 = Tomcat.addServlet(ctx,"em",new EmServlet());
        //容器启动时，完成初始化
        wrp3.setLoadOnStartup(1);
        //设置servlet请求路径
        wrp3.addMapping("/em");

        //注册servelt
        Wrapper wrp4 = Tomcat.addServlet(ctx,"stu",new StudentServlet());
        //容器启动时，完成初始化
        wrp4.setLoadOnStartup(1);
        //设置servlet请求路径
        wrp4.addMapping("*.do");
        try {
            //启动tomcat
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
